home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
The World of Computer Software.iso
/
pbaseiv.zip
/
P4MSC008.TIP
< prev
next >
Wrap
Text File
|
1991-12-16
|
3KB
|
91 lines
In a sense, a running BASIC program is lost--it doesn't know
its name or its path, a fact that poses a problem for
programs that must access files in their home directories. I
solved that problem by taking advantage of a little-known
DOS feature.
When DOS runs a program, it stores the program's path and
name after the last environment string. For a BASIC program
to find its roots, it must locate the Program Segment Prefix
(PSP), which is attached to the program at run time. From
the PSP, the program can find out the directory from which
it was loaded. It's then a simple matter to lop off the
path, attach the file names you need, and use the results to
access data in the program's home directory. I've included a
sample program [see listing below] that shows how this is
done. [You can copy the listing to a file by pressing the
Alt-F key.]
H. B. Clyde, Jr.
Incline Village, Nevada
Editor's note: I modified Mr. Clyde's program so you can
compile it with the popular QuickBASIC 4.5. When run, the
program displays its directory path and file name.
Before compiling, start QuickBASIC with the /L parameter so
that the Quick Library QB.QLM, which contains support
routines needed to call DOS functions, is also loaded. The
program uses one of those routines, CALL Interrupt, to
locate the PSP.
In the listing, comments beginning with an apostrophe
explain how the program works. The second line, however, is
not a comment but a command to include QB.BI, which defines
RegType, a structure that stores pseudo register values to
pass to DOS and to receive information back.
FINDPATH.BAS demonstrates how a QuickBASIC program can
locate its own path.
---- BEGIN LISTING ----
' FINDPATH by H. B. Clyde, Jr.
' $INCLUDE: 'QB.BI'
' Find Prog Seg Prefix (PSP)
DIM regs AS RegType
regs.ax = &H6200
CALL Interrupt(&H21,regs,regs)
' Locate program's environment
PSPSegment = regs.bx
def seg = PSPSegment
offset = &H2C
LoAdd = peek(offset)
HiAdd = peek(offset + 1)
EnvSeg = LoAdd + (256 * HiAdd)
' Find command string
def seg = EnvSeg
x = 0
while peek(x) + peek(x+1) <> 0
x = x + 1
wend
' Find end of program's path
StartField = x + 4
EndField = StartField
while peek(EndField) <> 0
EndField = EndField + 1
wend
' Locate backslash at path end
EndPath = EndField
while peek(EndPath) <> &H5C
EndPath = EndPath - 1
wend
' Copy path name to string
for x = StartField to EndPath
Path$ = Path$ + chr$(peek(x))
next x
print "Path = "Path$
' Copy program name to string
for x = EndPath + 1 to EndField
Prog$ = Prog$ + chr$(peek(x))
next x
print "Program = " Prog$
end
---- END LISTING ----
Title: Where Am I?
Category: MSC
Issue date: May 1991
Editor: Tom Swan
Supplementary files: NONE